package com.rcloud.server.sealtalk.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.rcloud.server.sealtalk.domain.UserAdvertisement;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.service.UserAdvertisementService;
import com.rcloud.server.sealtalk.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class AdvertisementTask {

    @Resource
    private UserAdvertisementService advertisementService;

    @Resource
    private UsersService usersService;

    /**
     * 广告认证的定时任务
     */
    @Async
    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨0点执行一次
    public void resetAdvertisementTask() {
        DateTime date = DateUtil.date();
        UserAdvertisement paramsAd = new UserAdvertisement();
        paramsAd.setRefreshNum(3);
        paramsAd.setEditNum(3);
        //业务处理
        log.info("执行广告刷新编辑的定时任务---------------------开始" + date);
        Example example = new Example(UserAdvertisement.class);
        example.createCriteria().andNotEqualTo("refreshNum", 3).orNotEqualTo("editNum", 3);
        List<UserAdvertisement> advertisementList = advertisementService.getByExample(example);
        for (UserAdvertisement advertisement : advertisementList) {
            //重置次数
            paramsAd.setId(advertisement.getId());
            advertisementService.updateByPrimaryKeySelective(paramsAd);
        }
        log.info("执行广告刷新编辑的定时任务---------------------结束" + date.getTime());
    }

    /**
     * 广告置顶失效的定时任务
     */
    @Async
    @Scheduled(cron = "5 0 0 * * ?")//每天凌晨0点执行一次
    public void resetTopAdvertisementTask() {
        DateTime date = DateUtil.date();
        //业务处理
        log.info("执行广告置顶失效的定时任务---------------------开始" + date);
        Example example = new Example(UserAdvertisement.class);
        example.createCriteria().andEqualTo("isTop", 1);
        List<UserAdvertisement> advertisementList = advertisementService.getByExample(example);
        for (UserAdvertisement advertisement : advertisementList) {
            //如果当前时间已经超过置顶的时间，那么就进行关闭
            if (date.getTime() > advertisement.getToppingExpireTime().getTime()) {
                //置顶取消
                advertisementService.updateIsTopAdvertisement(advertisement.getId());
            }

        }
        log.info("执行广告置顶失效的定时任务---------------------结束" + date.getTime());
    }

    /**
     * 重置用户发布的次数
     */
//    @Async
//    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨0点执行一次
    public void resetUserPubAdvertisementTask() {
        Users paramsUser = new Users();
        paramsUser.setAvailableTime(5);
        DateTime date = DateUtil.date();
        //业务处理
        log.info("执行广告发布的定时任务---------------------开始" + date);
        Example example = new Example(Users.class);
        example.createCriteria().andNotEqualTo("availableTime", 5);
        List<Users> usersList = usersService.getByExample(example);
        for (Users users : usersList) {
            //重置次数
            paramsUser.setId(users.getId());
            usersService.updateByPrimaryKeySelective(paramsUser);
        }
        log.info("执行广告发布的定时任务---------------------结束" + date.getTime());
    }


}
